// Copyright by Scot Drysdale

package cn.edu.nju.software.grapheditor.shape;

import java.awt.*;
/**@author smy*/
//Completed on May 21th
//Modified on May 28th
public class Oval extends Shape {
    private int x; //Location of the upper left corner
    private int y;
    private int width;   //How "big" of the oval
    private int height;
    private int centerx;
    private int centery;
    
	public Oval(Color c,int x,int y,int width,int height) {
		super(c);
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
		this.centerx=x+width/2;
		this.centery=y+height/2;
	}

	// Helper method that returns whether Point p is in an Oval with the given
	// top left corner and size.
	private static boolean pointInOval(Point p, int left, int top, int width,
			int height) {
		double a = width / 2.0; // half of the width
		double b = height / 2.0; // half of the height
		double centerx = left + a; // x-coord of the center
		double centery = top + b; // y-coord of the center
		double x = p.x - centerx; // horizontal distance between p and center
		double y = p.y - centery; // vertical distance between p and center

		// Now we just apply the standard geometry formula.
		// (See CRC, 29th edition, p. 178.)
		return Math.pow(x / a, 2) + Math.pow(y / b, 2) <= 1;
	}

	@Override
	public boolean containsPoint(Point p) {
		return pointInOval(p,(this.centerx-this.width/2),(this.centery-this.height/2),this.width,this.height);
	}

	@Override
	public void drawShape(Graphics page) {
		page.drawOval(x, y, width, height);
		page.fillOval(x, y, width, height);
	}

	@Override
	public Point getCenter() {
		Point ans=new Point();
		ans.x=this.centerx;
		ans.y=this.centery;
		return ans;
	}

	@Override
	public void move(int deltaX, int deltaY) {
		this.centerx+=deltaX;
		this.centery+=deltaY;
		this.x=this.centerx-width/2;
		this.y=this.centery-height/2;
	}

	@Override
	public String getclass() {
		return "Oval";
	}
	
	public int getx(){
		return this.x;
	}
	
	public int gety(){
		return this.y;
	}
	
	public int getwidth(){
		return this.width;
	}
	
	public int getheight(){
		return this.height;
	}
}
